package org.example.mybatisdemo2.mapper;

import org.apache.ibatis.annotations.*;
import org.example.mybatisdemo2.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {
//    查询的接口
    @ResultMap("BaseMap")
    @Select("select * from userinfo where id = 1")
    List<UserInfo> queryUserInfos();

//    参数的传递
//  @Select("select * from userinfo where id = #{value}")   -->如果只有一个参数，默认取值时，参数名称可以任意定义
//  UserInfo queryUserInfo(@Param("id") Integer id);

    @Select("select * from userinfo where id = #{id}")
    UserInfo queryUserInfo(@Param("id") Integer id);

    @Select("select * from userinfo where id = #{value} ")
    UserInfo queryUserInfoByIdAndName(Integer id,String name);

    @Select("select * from userinfo where id = #{param1} and username = #{name}")
    UserInfo queryUserInfoByIdAndName2(@Param("id") Integer id,@Param("name") String name);

//    数据的插入
//    上面：sql语句，要和数据库字段名称对应
//    下面：和属性名称保持一致
//    获取自增id
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username,`password`,age,gender,phone,delete_flag)" +
            "values (#{username},#{password},#{age},#{gender},#{phone},#{delete_flag})")
    Integer insert(UserInfo userInfo);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo (username,`password`,age,gender,phone,delete_flag)" +
            "values (#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone},#{userInfo.delete_flag})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userinfo where id = #{id}")
    Integer deleteById(Integer id);

    @Update("update userinfo set `password` = #{password} where id = #{id}")
    Integer updateById(UserInfo userInfo);

//    如果数据库字段名与实体类属性名不同，可以通过取别名的方式来进行修改一致
    @Select("select id, username, password, age, gender, phone," +
            " delete_flag as deleteflag, create_time as createtime, update_time as updatetime from userinfo")
    List<UserInfo> queryUserInfos2();

//    注解
//    @Results({
//            @Result(column = "delete_flag",property = "deleteflag"),
//            @Result(column = "create_time",property = "createtime"),
//            @Result(column = "update_time",property = "updatetime"),
//    })
//    @Select("select * from userinfo")
//    List<UserInfo> queryUserInfos3();
    @Results(id = "BaseMap", value = {
            @Result(column = "delete_flag",property = "deleteflag"),
            @Result(column = "create_time",property = "createtime"),
            @Result(column = "update_time",property = "updatetime"),
    })

    @Select("select * from userinfo")
    List<UserInfo> queryUserInfos3();

    @Select("select * from userinfo where username = #{username}")
    UserInfo queryUserInfoByName1(@Param("username") String username);

    @Select("select * from userinfo where username = ${username}")
    UserInfo queryUserInfoByName2(@Param("username") String username);

}



